package chapter08;

public class Java08_Thread_Safe {
    public static void main(String[] args) throws Exception{
        // TODO Java 线程 -  线程安全
        // 多个线程并发执行时，修改了共享内存中共享对象的属性，导致数据冲突问题
        User8 user = new User8();

        Thread t1 = new Thread(()-> {
            user.name = "zhangsan";
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(user.name);
        });

        Thread t2 = new Thread(()-> {
            user.name = "lisi";
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(user.name);
        });

        t1.start();
        t2.start();

        System.out.println("main线程执行完毕");

        /* 线程安全问题
        main线程执行完毕
        lisi
        lisi
         */
    }
}
class User8 {
    public String name;
}
